<%@page import="javax.activation.FileTypeMap"%>
<%@page import="com.wodwl.configuration.ResourceConfiguration"%>
<%@page import="com.wodwl.entity.Resource"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="com.wodwl.entity.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<%@include file="../templete/header.jsp" %>
    <script type="text/javascript" src="../../static/js/resource_grid.js"></script>
	<style type="text/css">
		.newspaper
{
	font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
	font-size: 12px;
    width: 100%;
	text-align: left;
	border-collapse: collapse;
	/* border: 1px solid #69c; */
}
.newspaper th
{
	padding: 7px 17px 7px 17px;
	font-weight: normal;
	font-size: 14px;
	color: #039;
	/*border-bottom: 1px dashed #69c;*/
	border-top: 1px solid #EBEBEB;
	border-bottom: 1px solid #EBEBEB;
}
.newspaper tr
{
	border-bottom: 1px solid #EBEBEB;
	border-collapse: collapse;
}
.newspaper td
{
	padding: 7px 17px 7px 17px;
	color: #669;
}
.newspaper tbody tr:hover td
{
	color: #339;
	background: #F5F9FF;
}

.list-control 
{
padding: 7px 17px;
}

.button
{        
	display: inline-block;
	white-space: nowrap;
	background-color: #eee;
	background-image: -webkit-gradient(linear, left top, left bottom, from(#fff), to(#eee));
	background-image: -webkit-linear-gradient(top, #fff, #eee);
	background-image: -moz-linear-gradient(top, #fff, #eee);
	background-image: -ms-linear-gradient(top, #fff, #eee);
	background-image: -o-linear-gradient(top, #fff, #eee);
	background-image: linear-gradient(top, #fff, #eee);
	filter: progid:DXImageTransform.Microsoft.gradient(startColorStr='#eeeeee', EndColorStr='#eeeeee');
	border: 1px solid rgb(220, 220, 220);
	padding: 0 1.5em;
	margin: 0.5em;
	/* font: bold 1em/2em Microsoft YaHei, Arial, Helvetica; */
	font: 1em/2em Microsoft YaHei, Arial, Helvetica;
	text-decoration: none;
	color: #333;
	text-shadow: 0 1px 0 rgba(255,255,255,.8);
	-moz-border-radius: .2em;
	-webkit-border-radius: .2em;
	border-radius: .12em;
	-moz-box-shadow: 0 0 1px 1px rgba(255,255,255,.8) inset, 0 1px 0 rgba(0,0,0,.3);
	-webkit-box-shadow: 0 0 1px 1px rgba(255,255,255,.8) inset, 0 1px 0 rgba(0,0,0,.3);
	box-shadow: 0 0 1px 1px rgba(255,255,255,.8) inset, 0 1px 0 rgba(0,0,0,.3);

}
a.button{
text-decoration: none;
}
.button:hover
{
	background-color: #eee;        
	background-image: -webkit-gradient(linear, left top, left bottom, from(#fafafa), to(#ddd));
	background-image: -webkit-linear-gradient(top, #fafafa, #ddd);
	background-image: -moz-linear-gradient(top, #fafafa, #ddd);
	background-image: -ms-linear-gradient(top, #fafafa, #ddd);
	background-image: -o-linear-gradient(top, #fafafa, #ddd);
	background-image: linear-gradient(top, #fafafa, #ddd);
	filter: progid:DXImageTransform.Microsoft.gradient(startColorStr='#fafafa', EndColorStr='#dddddd');        
}

.button:active
{
	-moz-box-shadow: 0 0 4px 2px rgba(0,0,0,.3) inset;
	-webkit-box-shadow: 0 0 4px 2px rgba(0,0,0,.3) inset;
	box-shadow: 0 0 4px 2px rgba(0,0,0,.3) inset;
	position: relative;
	top: 1px;
}

.button:focus
{
	outline: 0;
	background: #fafafa;
}    

.button:before
{
	background: #eee;
	background: rgba(0,0,0,.1);
	float: left;        
	width: 1em;
	text-align: center;
	font-size: 1.5em;
	margin: 0 1em 0 -1em;
	padding: 0 .2em;
	-moz-box-shadow: 1px 0 0 rgba(0,0,0,.5), 2px 0 0 rgba(255,255,255,.5);
	-webkit-box-shadow: 1px 0 0 rgba(0,0,0,.5), 2px 0 0 rgba(255,255,255,.5);
	box-shadow: 1px 0 0 rgba(0,0,0,.5), 2px 0 0 rgba(255,255,255,.5);
	-moz-border-radius: .15em 0 0 .15em;
	-webkit-border-radius: .15em 0 0 .15em;
	border-radius: .15em 0 0 .15em;
        pointer-events: none;        
}

/* Hexadecimal entities for the icons */

.add:before
{
	content: "\271A";
}

.edit:before
{
	content: "\270E";        
}

.delete:before
{
	content: "\2718";        
}

.save:before
{
	content: "\2714";        
}

.email:before
{
	content: "\2709";        
}

.like:before
{
	content: "\2764";        
}

.next:before
{
	content: "\279C";
}

.star:before
{
	content: "\2605";
}

.spark:before
{
	content: "\2737";
}

.play:before
{
	content: "\25B6";
} 
.doclist-col-checkbox{
    height: 15px;
    outline: 0px none;
    width: 15px;
    left: 0px;
    position: relative;
    top: -3px;
}

#DynamicGridLoading { width: 160px; margin: 0 auto; padding: 0; background-color: #fff; text-align: center; border: solid 1px #000; }
#DynamicGridLoading img { margin: 20 auto; vertical-align: middle; }
		
	</style>
	<script type="text/javascript">
	var jsonData;
	var dashData;
	var curView=2;//1:列表模式  2：网格模式
	
	$(document).ready(request("a-1"));
	
	
	function request(path){
		$.ajax({       
			type: "POST",   
			url: "/resource/resource_list.do?path="+path, 
			contentType: "application/json; charset=utf-8", 
			dataType: "json",       
			data: "",  
			success: function(res) { 
				jsonData=res;
				$('#DynamicGridLoading').hide();
				refresh(2);
			}   
			});
	}
	function switchView(view){
		curView=view;
		if(curView==1){
			$('#DynamicGrid').slideUp();
			$('#DynamicList').slideDown();
		}else if(curView==2){
			$('#DynamicList').slideUp();
			$('#DynamicGrid').slideDown();
		}
		
	}
	
	function refresh(view){
		curView=view;
		var item=document.getElementById("DynamicList");
		item.innerHTML=CreateTableView(jsonData.resources, "newspaper", true);
		item=document.getElementById("DynamicGrid");
		item.innerHTML=createGrid(jsonData.resources,jsonData.path);
		if(curView==1){
			switchView(curView);
		}else if(curView==2){
			switchView(curView);
		}
	}
	
	function del(rid,type,path){
		/*
		var flag=confirm("确定要删除文件？");
		if(!flag)
			return;
		*/
		for(var i=0;i<jsonData.resources.length;i++)
			if(jsonData.resources[i].rid==rid){
				jsonData.resources.splice(i,1);
				var xmlhttp=getXmlHttp();
				xmlhttp.open("GET", "/resource/delete.do?rid="+rid+"&type="+type+"&path="+path, true);
				xmlhttp.send();
				break;
			}
		refresh(curView);
		
	}
	

	function rename1(rid,pid){
		var item=document.getElementById(rid+"_name");
		var inputBox;
		if(item.innerHTML.indexOf("<input")!=-1){//防止重复点击“重命名”按钮
			inputBox=document.getElementById("input_"+rid);
			inputBox.select();
			inputBox.focuse();
			return;
		}
		item.innerHTML="<input id=\"input_"+rid+"\" type=\"text\" value=\""+item.innerHTML+"\" onkeydown=\"rename2(this.value,"+rid+","+pid+")\">";
		inputBox=document.getElementById("input_"+rid);
		inputBox.select();
		inputBox.focuse();
	}
	function rename2(name,rid,pid){
		if(event.keyCode==13&&name!=""){
			var item=document.getElementById(rid+"_name");
			item.innerHTML=name;//更新页面提升用户体验
			for(var i=0;i<jsonData.resources.length;i++)
				if(jsonData.resources[i].rid==rid){
					jsonData.resources[i].name=name;
					break;
				}
			//发送异步数据删除服务器数据
			var xmlhttp=getXmlHttp();
			xmlhttp.open("GET", "/resource/rename.do?rid="+rid+"&name="+encodeURI(encodeURI(name)), true);
			xmlhttp.send();
		}
	}
	function addFolder(path){
		var name=prompt("请输入新建文件夹名称","新建文件夹");
		$.ajax({       
			type: "GET", 
			async:false,
			url: "/resource/add_folder.do?path="+path+"&name="+encodeURI(encodeURI(name)), 
			contentType: "application/json; charset=utf-8", 
			dataType: "json",       
			data: "",  
			success: function(res) { 
				try{
					jsonData=res;
					refresh(curView);
				}catch(error){
					alert(error);
				}
			}   
			});
	}
	
	function getXmlHttp(){
		var xmlhttp;
		if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp = new XMLHttpRequest();
		} else {// code for IE6, IE5
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		return xmlhttp;
	}
	function upload(){
		window.location.href="/resource/resource_add_view.do?path="+jsonData.path;
	}
	function show_resource(rid){
		//window.location.href="/jsp/resource/resource_show.jsp?rid="+rid;
		var top=document.getElementById("top");
		var item;
		for(var i=0;i<jsonData.resources.length;i++)
			if(jsonData.resources[i].rid==rid){
				item=jsonData.resources[i];
				break;
			}
		var str="<ul>";
		str+="<li>"+"<img src='/img/file_type/word.png' >"+"</li>"
		str+="<li>"+"名称："+item.name+"</li>"
		str+="<li>"+"类型："+item.type+"</li>"
		str+="<li>"+"大小："+item.size+"</li>"
		str+="<li>"+"上传者："+item.owner+"</li>"
		str+="<li>"+"上传时间："+item.create_time+"</li>"
		str+="<li>"+"下载次数："+item.download_count+"</li>"
		str+="<li>"+"<a href='<%=ResourceConfiguration.resource_downloadPath%>"+item.store_relative_path+"'>下载</a>"+"</li>"
		str+="</ul>";
		
		top.innerHTML=str;
		
	}
	function dash(){
		$.ajax({       
			type: "GET", 
			async:false,
			url: "/resource/get_droped_resources.do", 
			contentType: "application/json; charset=utf-8", 
			dataType: "json",       
			data: "",  
			success: function(res) { 
				dashData=res;
				try{
					showDashView(dashData.resources);
				}catch(error){
					alert(error);
				}
			}   
			});
	}
	function restore(rid,path){
		for(var i=0;i<dashData.resources.length;i++)
			if(dashData.resources[i].rid==rid)
				dashData.resources.splice(i,1);
		showDashView(dashData.resources);
		var xmlhttp=getXmlHttp();
		xmlhttp.open("GET", "/resource/restore.do?path="+path+"a"+rid);
		xmlhttp.send();
	}
	function showDashView(objArray){
		$('#DynamicGrid').slideUp();
		$('#DynamicGrid').slideUp();
		$('#listtype').hide();
		var str="回收站<hr>";
		str+='<table><tbody><tr>';
		 var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
		 for (var i = 0; i < array.length; i++) {
		    	var item=array[i];
		    	if(item.type==4){
		    		str+='<td align="center">\
		    			<p>\
						<img id="img_type" src="../../static/img/file_type/folder.png"  >\
						</p>\
						<p id="'+item.rid+'_name"> '+item.name+'</p>\
						<p id="'+item.rid+'_name"> '+item.last_modify_time+'</p>\
						<a href="#" onclick="restore(\''+item.rid+'\',\''+item.path+'\')">还原</a>\
						</td>';
				
		    	}else{
		    		str+='<td>\
						<img id="img_type" src="../../static/img/file_type/word.png" align="left" >\
						名称： '+item.name+'<br/>\
						大小：'+item.size+'<br/>\
						 类别：程序文档<br/> \
		    			上传者：'+item.owner+'<br/>\
		    			<a href="#" onclick="restore(\''+item.rid+'\',\''+item.path+'\')">还原</a>\
					</td>';
		    	}
		    	if((i+1)%4==0){
		    		str+='</tr><tr>';
		    	}
		    }
		
			str+='</tr></tbody></table>';
			var dashView=document.getElementById("Dash");
			dashView.innerHTML=str;
		
	}
	
	</script>
</head>
<body >
<%pageContext.setAttribute("linkType","5");%>
<%@include file="../templete/user_info.jsp"  %>
	<% 
	//List<Resource> resources=(List<Resource>) request.getAttribute("resources");
	//String path=(String)request.getAttribute("path");
	List<Resource> resources=new ArrayList<Resource>();
	String path="a-1";

%>
		

		
		<div id="top" class="conR">
		<p  id="listtype" >
			<button  onclick="switchView(1);" >列表模式</button>|
			<button onclick="switchView(2);">网格模式</button>
		</p>	
        <div id="DynamicList" >  </div>
        <div id="DynamicGrid" ></div>
        <div id="Dash" ></div>
	
		</div>


<jsp:include page="../templete/footer.jsp"></jsp:include>

</body>
</html>